package com.grt.condify.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grt.condify.entity.Person;
import com.grt.condify.mapper.PersonMapper;
import com.grt.condify.service.PersonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {
    private static final Logger logger = LoggerFactory.getLogger(PersonServiceImpl.class);

    @Autowired
    private PersonMapper personMapper;

    @Override
    public IPage<Person> queryForPage(IPage<Person> page, QueryWrapper<Person> queryWrapper) {
        return personMapper.selectPage(page, queryWrapper);
    }

    @Transactional(rollbackFor={RuntimeException.class, Exception.class})
    public void transPerson(Person person) throws Exception {

        person.setName("测试事务相关service1");
        saveOrUpdate(person);

        Thread.sleep(50000);
        System.out.println("主线程service睡50秒");

        if (person.getPersonId() == 6){
            throw new Exception("我抛个错看事务回退！");
        }

        person.setName("测试事务相关service2");
        saveOrUpdate(person);
    }

    @Override
    //@EventListener(eventClass = MyTestEvent.class,message = "person的service层getById方法")
    public Person getById(Integer id) {
        return super.getById(id);
    }
}
